package com.comtime.utils;

import java.io.IOException;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;

public class MusicPlayer implements OnCompletionListener {
	private Vibrator mVibrator;

	int repeatTotalCount = 3;// 总次�?

	int playedCount;// 已播放次�?

	AudioManager am;
	int iOldVolume = -1;
	private MediaPlayer mediaPlayer;

	Context mContext;

	
	private OnPlayStateListener onPlayStateListener;
	public MusicPlayer(Context mContext) {
		super();
		this.mContext = mContext;
		this.mediaPlayer = new MediaPlayer();
		mediaPlayer.setOnCompletionListener(this);
	}

	public void playWithTimesAndPath(int times, String path,boolean needVibrate,boolean maxVoice) {
		if (am == null) {
			am = (AudioManager) mContext
					.getSystemService(Service.AUDIO_SERVICE);
		}
		iOldVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);

		if(maxVoice){
			am.setStreamVolume(AudioManager.STREAM_MUSIC,
					am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
					AudioManager.FLAG_PLAY_SOUND);
		}else {
			if (am.getStreamVolume(AudioManager.STREAM_SYSTEM) == 0) {// 静音模式
				am.setStreamVolume(AudioManager.STREAM_MUSIC, 0,
						AudioManager.FLAG_PLAY_SOUND);
			} else {
				if (iOldVolume < am
						.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 2) {
					am.setStreamVolume(
							AudioManager.STREAM_MUSIC,
							am.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 2,
							AudioManager.FLAG_PLAY_SOUND);
				}
			}
		}
		this.repeatTotalCount = times;
		int duration = playWithPath(path);
		if(needVibrate){
			duration = repeatTotalCount * duration;
			duration = duration / 1000;
			playVibrate(duration);
		}
		Log.e("tag", "repeatTotalCount:"+repeatTotalCount);
		playedCount = 0;
	}

	public void stopPlay() {
		mediaPlayer.stop();
		mediaPlayer.reset();
		stopVibrate();
	}

	public void setOnPlayStateListener(OnPlayStateListener onPlayStateListener){
		this.onPlayStateListener=onPlayStateListener;
	}
	
	public int playWithPath(String path) {
		stopPlay();
		try {
			mediaPlayer.setDataSource(mContext, Uri.parse(path));
			mediaPlayer.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int duration = mediaPlayer.getDuration();
		
		if (duration < 1000) {
			duration = 1000;
		}

		mediaPlayer.start();

		return duration;
	}

	// 时间长度，秒
	private void playVibrate(int duration) {
		mVibrator = (Vibrator) mContext
				.getSystemService(Service.VIBRATOR_SERVICE);
		int length = duration * 2;
		long[] pattern = new long[length];
		for (int i = 0; i < length; i++) {
			if (i % 2 == 0) {
				pattern[i] = 200;
			} else {
				pattern[i] = 800;
			}
		}
		Log.e("", " playVibrate");
		mVibrator.vibrate(pattern, -1);
	}

	private void stopVibrate() {
		if (mVibrator != null)
			mVibrator.cancel();
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		playedCount++;
		Log.e("tag", "repeatTotalCount:"+repeatTotalCount+"playedCount:"+playedCount);
		if (playedCount < repeatTotalCount) {
			mediaPlayer.start();
		} else {
			mediaPlayer.reset();
			
			if(onPlayStateListener!=null){
				onPlayStateListener.onCompletion(this);
			}
			if(iOldVolume!=-1&&am!=null){
			am.setStreamVolume(AudioManager.STREAM_MUSIC,
					(int) iOldVolume, AudioManager.FLAG_PLAY_SOUND);
			}
		}
	}

	
	
	public interface OnPlayStateListener{
		public void onCompletion(MusicPlayer musicPlayer) ;
		
	}
	
}
